package cn.hpclub.server.model;

import java.util.List;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Model;

import cn.hpclub.server.util.InterfaceUtil;

/**
 * 实体类 - 角色
 * 
 */
@TableBind(tableName = "sys_role")
public class SysRole extends Model<SysRole>{

    private static final long   serialVersionUID = 370802114261166318L;
    public static final SysRole dao              = new SysRole();

    /**
     * 获取所有Role实体对象集合.
     * 
     * @return Role实体对象集合
     */
    public List<SysRole> getAllRole(){
        return dao.find("select * from sys_role where is_deleted=0");
    }

    public List<SysRole> getActiveRoleListByLevel(){
        String sql = "select * from sys_role where is_deleted=0 and is_active=1 and level>=(select level from sys_role where id=?)";
        return dao.find(sql, getAdminRoleId());
    }

    private int getAdminRoleId(){
        int adminId = InterfaceUtil.getAdminId();
        SysUserRole userRole = SysUserRole.dao.findFirst("select role_id from sys_user_role where user_id=?", adminId);
        int adminRoleId = userRole.getInt("role_id");
        return adminRoleId;
    }

}
